home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / fweb / fweb-1.40 / demos / breakpt.web < prev    next >
Text File  |  1993-10-29  |  4KB  |  146 lines

  1. @z --- breakpt.web ---
  2.  
  3. THIS CODE SERVES AS AN EXAMPLE OF A C CODE WRITTEN IN WEB.
  4. HOWEVER, ITS CONTENTS IS FOR ADVANCED PROGRAMMERS ONLY! See the
  5. discussion of breakpointing/debugging in the user's manual.
  6.  
  7. @x-----------------------------------------------------------------------------
  8.  
  9. @c
  10.  
  11. \Title{BREAKPT.WEB}
  12.  
  13. @* DEBUGGING. This sample program shows how to write module breakpointing
  14. functions that work in conjunction with the general |_BP| macro mechanism.
  15.  
  16. @d NO 0
  17. @d YES 1
  18.  
  19. @ We begin by defining a structure~|_BP| that contains information about each
  20. breakpoint---a flag |on| to indicate whether that breakpoint is active or
  21. not, and a pointer to the name of the module. We will have an array |bps|
  22. of such structures, one for each possible section.
  23.  
  24. @a
  25. typedef struct
  26.         {
  27.         char on; /* Flag to indicate active or not. */
  28.         char *name; /* Name of the module. */
  29.         } BP;
  30.  
  31. /* The following illustrates use of the built-in macros |_MODULES|,
  32. |_SECTIONS|, ANd |_eval|. */
  33. BP bps0[_MODULES]; /* This array corresponds to all the independent module
  34.         names, with 0~corresponding to the unnamed module. */
  35. BP bps[_EVAL(_SECTIONS+1)]; /* These correspond to the \.{@@\ } or~\.{@@*}
  36.         sections, 
  37.         numbered as in \.{WEAVE}'s output. The zeroth section does
  38.         not exist. */ 
  39.  
  40. @ We shall add a module number to the breakpoint list by calling~|bp|
  41. \It{from the debugger} with a positive argument; we remove one from the
  42. list by calling with a negative argument.
  43.  
  44. @a
  45. int bp(int m)
  46. {
  47. /*  Check for |m| out-of-bounds. */
  48. if(abs(m) > _SECTIONS)
  49.     {
  50.     printf("Section number %d too large; must be < %d\n",
  51.         m,_SECTIONS);
  52.     return NO;
  53.     }
  54.  
  55. if(m > 0)
  56.     {
  57.     printf("Setting breakpoint at section %d\n",m);
  58.      return bps[m].on = YES;
  59.     }
  60. else if(m < 0)
  61.     {
  62.     printf("Removing breakpoint at section %d\n",-m);
  63.      return bps[-m].on = NO;
  64.     }
  65. else
  66.     {
  67.     puts("Removing all breakpoints");
  68.     for(; m<=_SECTIONS; m++)
  69.         bps[m].on = NO;
  70.     return NO;
  71.     }
  72. }
  73.  
  74. @ We can initialize all breakpoints by calling the following routine from
  75. the debugger. To make it easier for us in this demo, we do that explicitly
  76. in |main|.
  77. @a
  78. void init_bp(void)
  79. {
  80. int m;
  81.  
  82. puts("Initializing all breakpoints");
  83. for(m=0; m<=_SECTIONS; m++) bps[m].on = YES;
  84. }
  85.  
  86. @ The actual breakpoint macro should in general be defined as follows:
  87. \.{-m"\_BP(m,name)=trap(m,name);"}, where |trap|~is a function that is to
  88. be called at the beginning of each module.  If the module is in the
  89. breakpoint list, then |trap| will call the null function~|_bp|, on which
  90. one can set a debugger breakpoint. (This might be put into an
  91. initialization file.) Thus, for an active module breakpoint, the |trap|
  92. routine will print the name of the module, then stop in the debugger.
  93.  
  94. @a
  95. void _bp(void)
  96. {}
  97.  
  98. void trap(int m,char *name)
  99. {
  100. if(bps[m].on)
  101.         {
  102.         printf("Entering section %d, module \"%s\"\n",
  103.         m,bps[m].name = name);
  104.         _bp();
  105.         }
  106. }
  107.  
  108. @ Here is a test program.
  109. @a
  110. main()
  111. {
  112. int k = 50; /* To prevent an infinite loop if you run this without any
  113.         breakpoints set. */
  114.  
  115. init_bp(); /* Initialize all breakpoints. */
  116.  
  117. while(k-- > 0)
  118.         {
  119.         @<A@>;
  120.         @<B@>;
  121.         @<This is a very, very long module whose name seems never to end@>;
  122.         }
  123. }
  124.  
  125. @ A simple named section.
  126. @<A@>=
  127. {
  128. puts("A");
  129. }
  130.  
  131. @ This section has no braces, so the breakpointing mechanism won't work.
  132. @<B@>=
  133. puts("B");
  134.  
  135. @ This section starts off with a C~declaration. We must use the
  136. \.{@@\{}~command to prevent the debugging statement from being inserting
  137. before the declaration, and the \.{@@b}~command to say where to actually put
  138. the debugging statement.
  139. @<This...@>=
  140. @{
  141. int i;
  142.  
  143. @b
  144. puts("Testing");
  145. }
  146.